package com.jingdianjichi.subject.domain.Handler.subject;

import com.jingdianjichi.subject.common.enums.SubjectInfoTypeCodeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SubjectTypeHandlerFactory implements InitializingBean {

    @Resource
    private List<SubjectTypeHandler> subjectTypeHandlerList;

    Map<SubjectInfoTypeCodeEnum, SubjectTypeHandler> handlerMap = new HashMap<>();

    public SubjectTypeHandler getHandler(int subjectType) {
        SubjectInfoTypeCodeEnum subjectInfoTypeCodeEnum = SubjectInfoTypeCodeEnum.getByCode(subjectType);

        return handlerMap.get(subjectInfoTypeCodeEnum);
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        for (SubjectTypeHandler subjectTypeHandler:subjectTypeHandlerList){
            handlerMap.put(subjectTypeHandler.getHandlerType(),subjectTypeHandler);
        }
    }
}
